package com.study.scala

import scala.collection.mutable.ListBuffer

/**
  * List：元素可重复
  */
object ListDemo {

  def main(args: Array[String]): Unit = {

    // 定长List
    println("==============定长List=================")
    val list = List(1, 2, 3, 4, 5)
    println(list.head)
    println(list.tail)

    // Nil: 空的List
    // 1最为head，Nil作为tail，拼成一个List
    val l1 = 1 :: Nil
    println(l1)

    val l2 = 0 :: list
    println(l2)

    //可变List
    println("===============可变List===============")
    val listBuffer = ListBuffer[Int]()
    listBuffer += 1
    listBuffer += (2, 3)
    listBuffer ++= List(3, 4, 5)
    println(listBuffer.toList)
    listBuffer -= 2
    println(listBuffer.toList)
    println(listBuffer.isEmpty)

    println("=============== test sum ==============")
    println(sum())
    println(sum(1,2,3,4,5,6,7,8,9))
  }

  def sum(nums: Int*): Int = {
    if (nums.length == 0) {
      0
    } else {
      // :_*  =>  seq转成可变参数
      nums.head + sum(nums.tail: _*)
    }
  }
}
